<?php

class PageController extends BaseController {
    
    protected $layout = 'layouts.default';
    
    public function getIndex($category=null , $topic = null) {
        if(!is_null($topic)){
            $this->getTopicPage($topic);
        }else if(!is_null($category)){
            $this->getCategoryPage($category);
        }else {
            $this->getMainPage();
        }
    }
    
    public function getTest($category=null , $topic = null) {
        $this->layout->content = View::make('pages.about');
    }
    
    
    private function getTopicPage($topicSlug) {

        $topics = Topic::where('slug' , $topicSlug)->with('category' , 'seo' , 'contents')->orderBy('id' , 'desc')->get();
        
        $this->data['topic'] = $topics[0];
        $this->layout->title = $topics[0]->title;
        $this->layout->seo = $topics[0]->seo;
        $this->layout->head_title = $topics[0]->title;
        $this->layout->content = View::make('topic.content_list')->with($this->data);
    }
    
    private function getCategoryPage($categorySlug) {
        $category = Category::where('slug' , $categorySlug)->firstOrFail();
        $topics = $category->topics()->with('category' , 'seo' ,'contents' )->orderBy('id' , 'desc')->get();
        $seo = $category->seo->first();
        
        $this->data['topics'] = $topics;
        $this->layout->title  = $category->name;
        $this->layout->seo = $seo;
        
        $this->layout->content = View::make('topic.list')->with($this->data);
    }
    
    private function getMainPage() {
        $topics = Topic::orderBy('id' , 'desc')->get();
        $this->layout->content = View::make('topic.list')->with('topics' , $topics);
    }
    
}